﻿using System;
using System.Collections.Generic;
using System.Configuration;
using Eb;

namespace Es
{
    class ConfigCommon : ConfigurationSection
    {
        [ConfigurationProperty("Version", IsRequired = true)]
        public string Version
        {
            get { return (string)this["Version"]; }
            set { this["Version"] = value; }
        }
    }

    class ConfigCoCluster : ConfigurationSection
    {
        [ConfigurationProperty("ZkClientIp", IsRequired = true)]
        public string ZkClientIp
        {
            get { return (string)this["ZkClientIp"]; }
            set { this["ZkClientIp"] = value; }
        }

        [ConfigurationProperty("ZkClientPort", IsRequired = true)]
        public string ZkClientPort
        {
            get { return (string)this["ZkClientPort"]; }
            set { this["ZkClientPort"] = value; }
        }
    }

    class ConfigCoCouchbase : ConfigurationSection
    {
        [ConfigurationProperty("DllPath", IsRequired = true)]
        public string DllPath
        {
            get { return (string)this["DllPath"]; }
            set { this["DllPath"] = value; }
        }

        [ConfigurationProperty("BucketName", IsRequired = true)]
        public string BucketName
        {
            get { return (string)this["BucketName"]; }
            set { this["BucketName"] = value; }
        }

        [ConfigurationProperty("BucketPwd", IsRequired = true)]
        public string BucketPwd
        {
            get { return (string)this["BucketPwd"]; }
            set { this["BucketPwd"] = value; }
        }

        [ConfigurationProperty("User", IsRequired = true)]
        public string User
        {
            get { return (string)this["User"]; }
            set { this["User"] = value; }
        }

        [ConfigurationProperty("Pwd", IsRequired = true)]
        public string Pwd
        {
            get { return (string)this["Pwd"]; }
            set { this["Pwd"] = value; }
        }

        [ConfigurationProperty("Url", IsRequired = true)]
        public string Url
        {
            get { return (string)this["Url"]; }
            set { this["Url"] = value; }
        }
    }

    class ConfigCoRabbitMQ : ConfigurationSection
    {
        [ConfigurationProperty("HostName", IsRequired = true)]
        public string HostName
        {
            get { return (string)this["HostName"]; }
            set { this["HostName"] = value; }
        }

        [ConfigurationProperty("Port", IsRequired = true)]
        public int Port
        {
            get { return (int)this["Port"]; }
            set { this["Port"] = value; }
        }

        [ConfigurationProperty("UserName", IsRequired = true)]
        public string UserName
        {
            get { return (string)this["UserName"]; }
            set { this["UserName"] = value; }
        }

        [ConfigurationProperty("Password", IsRequired = true)]
        public string Password
        {
            get { return (string)this["Password"]; }
            set { this["Password"] = value; }
        }

        [ConfigurationProperty("VirtualHost", IsRequired = true)]
        public string VirtualHost
        {
            get { return (string)this["VirtualHost"]; }
            set { this["VirtualHost"] = value; }
        }
    }

    class ConfigCoUCenterSDK4Server : ConfigurationSection
    {
        [ConfigurationProperty("UCenterZkPath", IsRequired = true)]
        public string UCenterZkPath
        {
            get { return (string)this["UCenterZkPath"]; }
            set { this["UCenterZkPath"] = value; }
        }

        [ConfigurationProperty("UCenterMQHostName", IsRequired = true)]
        public string UCenterMQHostName
        {
            get { return (string)this["UCenterMQHostName"]; }
            set { this["UCenterMQHostName"] = value; }
        }

        [ConfigurationProperty("UCenterMQPort", IsRequired = true)]
        public int UCenterMQPort
        {
            get { return (int)this["UCenterMQPort"]; }
            set { this["UCenterMQPort"] = value; }
        }

        [ConfigurationProperty("UCenterMQUserName", IsRequired = true)]
        public string UCenterMQUserName
        {
            get { return (string)this["UCenterMQUserName"]; }
            set { this["UCenterMQUserName"] = value; }
        }

        [ConfigurationProperty("UCenterMQPassword", IsRequired = true)]
        public string UCenterMQPassword
        {
            get { return (string)this["UCenterMQPassword"]; }
            set { this["UCenterMQPassword"] = value; }
        }

        [ConfigurationProperty("UCenterMQVirtualHost", IsRequired = true)]
        public string UCenterMQVirtualHost
        {
            get { return (string)this["UCenterMQVirtualHost"]; }
            set { this["UCenterMQVirtualHost"] = value; }
        }
    }

    public class EsEngineConfig
    {
        //---------------------------------------------------------------------
        public string Version { get; private set; }
        public string ZkClientIp { get; private set; }
        public string ZkClientPort { get; private set; }
        public string MQHostName { get; private set; }
        public int MQPort { get; private set; }
        public string MQUserName { get; private set; }
        public string MQPassword { get; private set; }
        public string MQVirtualHost { get; private set; }
        public string CouchbaseDllPath { get; private set; }
        public string CouchbaseBucketName { get; private set; }
        public string CouchbaseBucketPwd { get; private set; }
        public string CouchbaseUser { get; private set; }
        public string CouchbasePwd { get; private set; }
        public string CouchbaseUrl { get; private set; }
        public string UCenterZkPath { get; private set; }
        public string UCenterMQHostName { get; private set; }
        public int UCenterMQPort { get; private set; }
        public string UCenterMQUserName { get; private set; }
        public string UCenterMQPassword { get; private set; }
        public string UCenterMQVirtualHost { get; private set; }

        //---------------------------------------------------------------------
        public EsEngineConfig()
        {
            Version = "1.00.000";

            ZkClientIp = "127.0.0.1";
            ZkClientPort = "2181";

            MQHostName = "127.0.0.1";
            MQPort = 5672;
            MQUserName = "";
            MQPassword = "";
            MQVirtualHost = "";

            CouchbaseDllPath = "";
            CouchbaseBucketName = "";
            CouchbaseBucketPwd = "";
            CouchbaseUser = "";
            CouchbasePwd = "";
            CouchbaseUrl = "";

            UCenterZkPath = "";
            UCenterMQHostName = "";
            UCenterMQPort = 0;
            UCenterMQUserName = "";
            UCenterMQPassword = "";
            UCenterMQVirtualHost = "";
        }

        //---------------------------------------------------------------------
        public void load()
        {
            ExeConfigurationFileMap file = new ExeConfigurationFileMap();
            file.ExeConfigFilename = EsEngine.Instance.Settings.EsEngineExeConfigFilename;
            System.Configuration.Configuration config =
                ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);

            ConfigCommon cfg_common = config.SectionGroups["EsEngine"].Sections["Common"] as ConfigCommon;
            Version = cfg_common.Version;

            ConfigCoCouchbase cfg_cocouchbase = config.SectionGroups["EsEngine"].Sections["CoCouchbase"] as ConfigCoCouchbase;
            CouchbaseDllPath = cfg_cocouchbase.DllPath;
            CouchbaseBucketName = cfg_cocouchbase.BucketName;
            CouchbaseBucketPwd = cfg_cocouchbase.BucketPwd;
            CouchbaseUser = cfg_cocouchbase.User;
            CouchbasePwd = cfg_cocouchbase.Pwd;
            CouchbaseUrl = cfg_cocouchbase.Url;

            ConfigCoCluster cfg_cocluster = config.SectionGroups["EsEngine"].Sections["CoCluster"] as ConfigCoCluster;
            ZkClientIp = cfg_cocluster.ZkClientIp;
            ZkClientPort = cfg_cocluster.ZkClientPort;

            ConfigCoRabbitMQ cfg_corabbitmq = config.SectionGroups["EsEngine"].Sections["CoRabbitMQ"] as ConfigCoRabbitMQ;
            MQHostName = cfg_corabbitmq.HostName;
            MQPort = cfg_corabbitmq.Port;
            MQUserName = cfg_corabbitmq.UserName;
            MQPassword = cfg_corabbitmq.Password;
            MQVirtualHost = cfg_corabbitmq.VirtualHost;

            if (EsEngine.Instance.Settings.EnableCoUCenterSDK4Server)
            {
                ConfigCoUCenterSDK4Server cfg_coucentersdk4server = config.SectionGroups["EsEngine"].Sections["CoUCenterSDK4Server"] as ConfigCoUCenterSDK4Server;
                UCenterZkPath = cfg_coucentersdk4server.UCenterZkPath;
                UCenterMQHostName = cfg_coucentersdk4server.UCenterMQHostName;
                UCenterMQPort = cfg_coucentersdk4server.UCenterMQPort;
                UCenterMQUserName = cfg_coucentersdk4server.UCenterMQUserName;
                UCenterMQPassword = cfg_coucentersdk4server.UCenterMQPassword;
                UCenterMQVirtualHost = cfg_coucentersdk4server.UCenterMQVirtualHost;
            }
        }

        //---------------------------------------------------------------------
        public void save()
        {
            ConfigCommon cfg_common = new ConfigCommon();
            cfg_common.Version = Version;

            ConfigCoCluster cfg_cocluster = new ConfigCoCluster();
            cfg_cocluster.ZkClientIp = ZkClientIp;
            cfg_cocluster.ZkClientPort = ZkClientPort;

            ConfigCoCouchbase cfg_cocouchbase = new ConfigCoCouchbase();
            cfg_cocouchbase.DllPath = CouchbaseDllPath;
            cfg_cocouchbase.BucketName = CouchbaseBucketName;
            cfg_cocouchbase.BucketPwd = CouchbaseBucketPwd;
            cfg_cocouchbase.User = CouchbaseUser;
            cfg_cocouchbase.Pwd = CouchbasePwd;
            cfg_cocouchbase.Url = CouchbaseUrl;

            ConfigCoRabbitMQ cfg_corabbitmq = new ConfigCoRabbitMQ();
            cfg_corabbitmq.HostName = MQHostName;
            cfg_corabbitmq.Port = MQPort;
            cfg_corabbitmq.UserName = MQUserName;
            cfg_corabbitmq.Password = MQPassword;
            cfg_corabbitmq.VirtualHost = MQVirtualHost;

            ConfigCoUCenterSDK4Server cfg_coucentersdk4server = new ConfigCoUCenterSDK4Server();
            cfg_coucentersdk4server.UCenterZkPath = UCenterZkPath;
            cfg_coucentersdk4server.UCenterMQHostName = UCenterMQHostName;
            cfg_coucentersdk4server.UCenterMQPort = UCenterMQPort;
            cfg_coucentersdk4server.UCenterMQUserName = UCenterMQUserName;
            cfg_coucentersdk4server.UCenterMQPassword = UCenterMQPassword;
            cfg_coucentersdk4server.UCenterMQVirtualHost = UCenterMQVirtualHost;

            ExeConfigurationFileMap file = new ExeConfigurationFileMap();
            file.ExeConfigFilename = "EsEngine.dll.config";
            System.Configuration.Configuration config =
                ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
            if (config.SectionGroups.Get("EsEngine") == null)
            {
                config.SectionGroups.Add("EsEngine", new ConfigurationSectionGroup());
            }

            if (config.SectionGroups["EsEngine"].Sections.Get("Common") == null)
            {
                config.SectionGroups["EsEngine"].Sections.Add("Common", cfg_common);
            }

            if (config.SectionGroups["EsEngine"].Sections.Get("CoCluster") == null)
            {
                config.SectionGroups["EsEngine"].Sections.Add("CoCluster", cfg_cocluster);
            }

            if (config.SectionGroups["EsEngine"].Sections.Get("CoCouchbase") == null)
            {
                config.SectionGroups["EsEngine"].Sections.Add("CoCouchbase", cfg_cocouchbase);
            }

            if (config.SectionGroups["EsEngine"].Sections.Get("CoRabbitMQ") == null)
            {
                config.SectionGroups["EsEngine"].Sections.Add("CoRabbitMQ", cfg_corabbitmq);
            }

            if (config.SectionGroups["EsEngine"].Sections.Get("CoUCenterSDK4Server") == null)
            {
                config.SectionGroups["EsEngine"].Sections.Add("CoUCenterSDK4Server", cfg_corabbitmq);
            }

            config.Save(ConfigurationSaveMode.Modified);
        }
    }
}
